package fr.umlv.m2.jee.pmlv.service.subject.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.m2.jee.pmlv.persistence.subject.AbstractTheme;
import fr.umlv.m2.jee.pmlv.persistence.subject.Subject;
import fr.umlv.m2.jee.pmlv.persistence.subject.dao.ISubjectDAO;
import fr.umlv.m2.jee.pmlv.service.subject.DisplaySubject;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplaySubjectService;
import java.util.Set;


@Service("defautDisplaySubjectService")
public class DefautDisplaySubjectService implements IDisplaySubjectService{
	
    @Autowired
    @Qualifier("hibernateSubjectDAO")
    private ISubjectDAO dao;

    @Override
    @Transactional(readOnly=true)
    public List<DisplaySubject> getAllDisplaySubject() {
        ArrayList< DisplaySubject > result = new ArrayList< DisplaySubject >(16);
        for(Subject subject : dao.findAll() ) {
            result.add(new DisplaySubject(subject));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplaySubject getDisplaySubjectbyId(long id) {
        return new DisplaySubject(dao.findById(id));
    }
    
    @Override
    @Transactional
    public void addAbstractTheme(Subject subject,AbstractTheme theme) {
    	Set<AbstractTheme> listTheme = subject.getThemes();
    	listTheme.add(theme);
    	subject.setThemes(listTheme);
    	dao.merge(subject);
    }
}
